package com.project.controller.system;

import com.project.controller.BaseController;
import com.project.dto.Result;
import com.project.dto.request.cmd.CmdLogin;
import com.project.dto.request.cmd.CmdRegister;
import com.project.dto.response.UserDTO;
import com.project.entity.User;
import com.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Date : 10:24 2020/7/1
 */
@RestController
@Api(tags = "登录、注册")
public class LoginController extends BaseController {
    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping(value = "/login", name = "用户登录")
    public Result<UserDTO> login(@RequestBody @Valid CmdLogin cmdLogin) {
        User user = userService.login(cmdLogin.getUsername(), cmdLogin.getPassword());
        return success(user, UserDTO.class);
    }

    @ApiOperation("注册")
    @PostMapping(value = "/register", name = "用户注册")
    public Result userRegister(@RequestBody @Valid CmdRegister cmdRegister) {
        userService.register(cmdRegister.getUsername(), cmdRegister.getRealName(), cmdRegister.getPassword(), cmdRegister.getPhone(), cmdRegister.getGrade());
        return success("注册成功");
    }
}
